package com.example.fixsys.controller;

import com.example.fixsys.common.Result;
import com.example.fixsys.entity.Reimburse;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController("reimburse")
public class ReimburseController {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @GetMapping("getReimburse")
    public Result<?> GetReimburse(){
        String sql = "select * from reimburse";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }
    @PutMapping("/addReimburse")
    public Result<?> AddReimburse(@RequestBody Reimburse reimburse){
        String sql="insert into reimburse(reimburseNo,fixWorker,things,prize,evidence,saler,date,reimburseStatus) VALUES (?,?,?,?,?,?,?,?)";
        try{
            jdbcTemplate.update(sql, new Object[]{reimburse.getReimburseNo(), reimburse.getFixWorker(), reimburse.getThings(), reimburse.getPrize(),reimburse.getEvidence(), reimburse.getSaler(), reimburse.getDate(), reimburse.getReimburseStatus()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/updateReimburse")
    public Result<?> UpdateReimburse(@RequestBody Reimburse reimburse){
        String sql="update reimburse set fixWorker=?,things=?,prize=?,evidence=?,saler=?,date=?,reimburseStatus=? where reimburseNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{reimburse.getFixWorker(), reimburse.getThings(), reimburse.getPrize(), reimburse.getEvidence(), reimburse.getSaler(), reimburse.getDate(), reimburse.getReimburseStatus(),reimburse.getReimburseNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteReimburse")
    public Result<?> DeleteReimburse(@RequestBody Reimburse reimburse){
        String sql="delete from reimburse where reimburseNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{reimburse.getReimburseNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }
}
